package com.cloudkinto.appletservice.stock.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.appletservice.stock.AppletStockTransferService;
import com.cloudkinto.appletservice.stock.vo.transfer.AppletStockTransferPageRes;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.*;
import com.cloudkinto.entity.*;
import com.cloudkinto.service.shop.ShopService;
import com.cloudkinto.service.stock.vo.transfer.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author 张永远
 * @since 2020-06-11
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class AppletStockTransferServiceImpl extends ServiceImpl<StockTransferDao, StockTransferDo> implements AppletStockTransferService {
    @Autowired
    private StockTransferDao dao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private StorageDao storageDao;
    @Autowired
    private UserDao userDao;



    @Override
    public PageResult getListPage(Map<String, String> map, Long userId) {
        QueryWrapper<StockTransferDo> wrapper = queryBuild(map);
        IPage<StockTransferDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<AppletStockTransferPageRes> resList = handlerListAfter(page.getRecords(), userId);
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<AppletStockTransferPageRes> handlerListAfter(List<StockTransferDo> list, Long userId) {
        List<AppletStockTransferPageRes> result = new ArrayList<>();
        list.forEach(e -> {
            AppletStockTransferPageRes res = new AppletStockTransferPageRes();
            res.setId(e.getId());

            //判断是否有权限
            //todo 预留如果是管理员有操作权限  且如果转出店铺=当前用户的shopId也有权限
            UserDo userDo = userDao.selectById(userId);
//            res.setAuthority(0);
//            if (userDo != null) {
//                if (userDo.getShopId() == e.getShopOutId()) res.setAuthority(1);
//                else if (userDo.getRoleId() == 1) res.setAuthority(1);
//            }
            UserDo userDo1 = userDao.selectById(e.getCreateBy());
            if (userDo1 != null)
                res.setCreateBy(userDo1.getUserName());
            ShopDo shopInDo = shopDao.selectById(e.getShopInId());
            if (shopInDo != null) {
                res.setShopInName(shopInDo.getShopName());
            }
            ShopDo shopOutDo = shopDao.selectById(e.getShopOutId());
            if (shopOutDo != null) {
                res.setShopOutName(shopOutDo.getShopName());
            }
//            StorageDo storageDo = storageDao.selectById(e.getStorageId());
//            if (storageDo != null) {
//                res.setStorage(storageDo.getStorageName());
//            }
            res.setCreateTime(e.getCreateTime());
//            res.setUpdateTime(e.getUpdateTime());
            if (e.getType() == 2) res.setType("已拒绝");
            else if (e.getType() == 1) res.setType("已确认");
            else res.setType("未确认");

            result.add(res);
        });
        return result;
    }

    private IPage<StockTransferDo> pageInit(Map<String, String> map) {
        IPage<StockTransferDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<StockTransferDo> queryBuild(Map<String, String> map) {
        QueryWrapper<StockTransferDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("shopOutId"))) {
            wrapper.eq("shop_out_id", map.get("shopOutId"));
        }
        if (StringUtils.isNotBlank(map.get("shopInId"))) {
            wrapper.eq("shop_in_id", map.get("shopInId"));
        }
        if (StringUtils.isNotBlank(map.get("storageId"))) {
            wrapper.eq("storage_id", map.get("storageId"));
        }
        wrapper.eq("company_id", map.get("companyId"));
        wrapper.orderByDesc("id");
        return wrapper;
    }


}
